<?php


namespace app\crontab\controller\once;


use custom\Mongo;
use custom\Redis;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 小学小区映射
 * Class SchoolPrimaryVillage
 * @package app\crontab\controller
 */
class SchoolPrimaryVillage extends Command {

    public static $setName = 'wx_xqf_xx_details_data';

    protected function configure() {
        $this->setName('SchoolPrimaryVillage');
    }

    protected function execute(Input $input, Output $output) {
        echo "start\n";
        $mongo = Mongo::getInstance();
        $int = 1;
        $options = [
            'projection' => ['_id' => 1, 'houses' => 1],
            'sort'       => ['id' => 1],
        ];
        $cursor = $mongo->select(self::$setName, [], $options);
        foreach ($cursor as $v1) {
            $v1 = (array)$v1;
            if ($v1['houses']) {
                foreach ($v1['houses'] as $v2) {
                    $v_name = $v2->name;
                    $vid = db("house_village")
//                        ->where('city_name', $v_name)
                        ->where('name', $v_name)
                        ->value('id');
                    if (!$vid) {
                        $vid = 0;
                    }
                    $mongo->add(
                        [
                            '_id'    => (int)$int,
                            'xx_id'  => (string)$v1['_id'],
                            'v_id'   => (int)$vid,
                            'v_name' => (string)$v_name
                        ],
                        'SchoolPrimaryVillage'
                    );
                    $int++;
                }
            }
            echo "end\n";
        }
    }

}